package app.sevenc.lib_framework.mvvm.v

import androidx.databinding.ViewDataBinding
import app.sevenc.lib_framework.ktx.getVmClazz
import app.sevenc.lib_framework.mvvm.vm.BaseFrameViewModel

/**
 * Mvvm设计模式 Fragment基类
 *
 * @author Seven-C
 * @date 2021/8/25
 */
abstract class BaseFrameMvvmFragment<VM : BaseFrameViewModel> : BaseFrameFragment(),
    IBaseFrameFragment {

    private lateinit var mBinding: ViewDataBinding
    val mViewModel: VM by lazy { createViewModel() }

    /**
     * 创建viewModel
     * type.actualTypeArguments[0]=BaseFrameViewModel
     * val type = javaClass.genericSuperclass as ParameterizedType
     * val vmClass = type.actualTypeArguments[0] as Class<VM>
     *
     */
    private fun createViewModel(): VM {
        return mFragmentProvider.get(getVmClazz(this, 0))
    }

    protected fun <T : BaseFrameViewModel> getActivityViewModel(modelClass: Class<T>): T {
        return mActivityProvider.get(modelClass)
    }
}